package xyz.xtt.equity.controller;

import java.util.List;
import java.util.Optional;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestHeader;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.media.Schema;
import jakarta.validation.Valid;
import jakarta.validation.constraints.NotBlank;
import lombok.extern.slf4j.Slf4j;
import xyz.xtt.common.response.ApiResult;
import xyz.xtt.equity.request.TlbLuckPhoneOrderReq;
import xyz.xtt.equity.response.TlbLuckPhoneOrderRes;
import xyz.xtt.equity.service.ITblLuckPhoneEntitlementDeliverService;
import xyz.xtt.equity.service.ITblLuckPhonePoolService;
import xyz.xtt.equity.service.ITblRegionService;
import xyz.xtt.equity.vo.LuckPhoneDeliverVo;
import xyz.xtt.equity.vo.LuckPhoneVo;
import xyz.xtt.equity.vo.PhoneRegionVo;

/**
 * 靓号rest 接口
 *
 * @author dale
 * @since 2024/12/31
 **/
@Schema(name = "靓号", description = "靓号")
@Slf4j
@RestController
@RequestMapping("/api/nft/equity/luckPhone")
@Validated
public class LuckPhoneController {
	@Autowired
	private ITblLuckPhonePoolService tblLuckPhonePoolService;
	@Autowired
	private ITblLuckPhoneEntitlementDeliverService tblLuckPhoneEntitlementDeliverService;
	@Autowired
	private ITblRegionService tblRegionService;

	@Operation(summary = "提交靓号订单")
	@PostMapping("/order")
	public ApiResult<TlbLuckPhoneOrderRes> submitLuckPhoneOrder(@RequestHeader("uid") String uid, @RequestBody @Valid TlbLuckPhoneOrderReq req) {
		return ApiResult.ok(tblLuckPhoneEntitlementDeliverService.submitLuckPhoneOrder(uid, req));
	}

	@GetMapping("/list")
	public ApiResult<List<LuckPhoneVo>> list(@RequestHeader("uid") String uid, @NotBlank(message = "权益ID不能为空") String id, Integer current, Integer pageSize,
			String city, String province) {
		current = Optional.ofNullable(current).filter(x -> x >= 1).orElse(1);
		pageSize = Optional.ofNullable(pageSize).filter(x -> x >= 1).orElse(10);

		return ApiResult.ok(tblLuckPhonePoolService.list(uid, id, current, pageSize, city, province));
	}

	@GetMapping("/getOrder")
	public ApiResult<LuckPhoneDeliverVo> getOrder(@RequestHeader("uid") String uid) {
		return ApiResult.ok(tblLuckPhoneEntitlementDeliverService.getLuckPhoneOrder(uid));
	}

	@Operation(summary = "获取靓号归属地列表")
	@GetMapping("/region")
	public ApiResult<List<PhoneRegionVo>> getRegionList(@RequestHeader("uid") String uid, @RequestParam(required = false) String parentId) {
		return ApiResult.ok(tblRegionService.getRegionList(parentId));
	}

}
